/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.user;

import com.servengine.portal.Component;
import com.servengine.portal.Portal;

import java.util.HashSet;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

@Entity
@NamedQueries ({
	@NamedQuery(name = "Role.findByName", query = "select o from Role o where o.portal.id = :portalid and o.name = :name"),
	@NamedQuery(name = "Role.findNewUserRoles", query = "select o from Role o where o.portal.id = :portalid and o.newUserRole = true"),
	@NamedQuery(name = "Role.findByClassName", query = "select r from Role r, in (r.actions) a where a.className=:className and r.portal.id = :portalid"),
	@NamedQuery(name = "Role.getUserCount", query = "select count(users) from Role as r join r.users as users WHERE r.id = :id"),
	@NamedQuery(name = "Role.getActionCount", query = "select count(actions) from Role as r join r.actions as actions WHERE r.id = :id")
})
@Table(uniqueConstraints = {@UniqueConstraint(columnNames={"name", "portal_id"})})
public class Role implements java.io.Serializable
{
	Integer id;
	String description, name, loginText;
	boolean newUserRole;
	Portal portal;
	Set<User> users;
	Set<Component> actions;

	long userCount,actionCount;
	String [] services, adminServices;//Estaba como un método, pero al ser Lazy no tiene mucho sentido.
	
	public Role() {}

	public Role(Portal portal, String name)
	{
		users = new HashSet<User>();
		actions = new HashSet<Component>();
		this.portal = portal;
		this.name = name;
	}

	public Role(Portal portal, String name, boolean newUserRole)
	{
		this(portal,name);
		this.newUserRole=newUserRole;
	}

	public boolean equals(Object o)
	{
		return ((Role)o).getName().equals(name) && ((Role)o).getPortal().getId().equals(portal.getId());
	}

	@Id @GeneratedValue
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}

/*	@PostLoad
	public void postLoad() //Genera la carga de los usuarios (!!!!)
	{
		userCount = users.size();
		actionCount = actions.size();
	}*/
	

	public boolean isNewUserRole()
	{
		return newUserRole;
	}

	public void setNewUserRole(boolean value)
	{
		newUserRole = value;
	}

	public java.lang.String getDescription()
	{
		return description;
	}

	public void setDescription(String value)
	{
		description = value;
	}

	public java.lang.String getName()
	{
		return name;
	}

	public String getLoginText()
	{
		return loginText;
	}

	public void setLoginText(String value)
	{
		loginText = value;
	}

	public void setUsers(Set<User> users)
	{
		this.users = users;
	}

	@ManyToMany(targetEntity=User.class,fetch=FetchType.LAZY, mappedBy="roles")
	public Set<User> getUsers()
	{
		return users;
	}

	public void setPortal(Portal portal)
	{
		this.portal = portal;
	}

	@ManyToOne(fetch=FetchType.EAGER, optional=false)
	public Portal getPortal()
	{
		return portal;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setActions(Set<Component> actions)
	{
		this.actions = actions;
	}

	@ManyToMany(targetEntity=Component.class,fetch=FetchType.LAZY)
	public Set<Component> getActions()
	{
		return actions;
	}

/*	@Transient
	public Set<String> getActionClassNames()
	{
		Set<String>actionNames = new HashSet<String>();
		for (Component action: actions)
			actionNames.add(action.getClassName());
		return actionNames;
	}*/
	@Transient
	public String[] getServices()
	{
		return services;
		/*List<String> services = new ArrayList<String>();
		for (Component action: actions)
			if (!action.isAdmin())
				services.add(action.getServiceName());
		String[] array = new String[services.size()];
		return services.toArray(array);*/
	}
	@Transient
	public String[] getAdminServices()
	{
		return adminServices;
	  /*List<String> services = new ArrayList<String>();
	  for (Component action: actions)
	    if (action.isAdmin())
	      services.add(action.getServiceName());
	  String[] array = new String[services.size()];
	  return services.toArray(array);*/
	}
	@Transient
	public long getActionCount()
	{
		return actionCount;
	}
	public void setActionCount(long actionCount)
	{
		this.actionCount = actionCount;
	}
	@Transient
	public long getUserCount()
	{
		return userCount;
	}

	public void setUserCount(long userCount)
	{
		this.userCount = userCount;
	}

	public void setServices(String[] services)
	{
		this.services = services;
	}

	public void setAdminServices(String[] adminServices)
	{
		this.adminServices = adminServices;
	}
}
